﻿// CPopTestTone.cpp: 实现文件
//

#include "pch.h"
#include "DntDemo.h"
#include "afxdialogex.h"
#include "CPopTestTone.h"
#include "CTabAudioKc3x.h"
#define TAG "TestTone"

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 标准C语言函数 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
CPopTestTone* mCPopTestTone = NULL;

static const char* Tab_DIP_ChannelShow[] = {				// 噪音测试和声调微调显示的字眼
	"前置左声道","前置右声道","中置","超低音","环绕左声道","环绕右声道","后置左声道","后置右声道"
};

static void changRegCallback() {
	if (mCPopTestTone != NULL) {
		mCPopTestTone->ShowRegister();
	}
}

int CPopTestTone::ShowChannel(uint8_t value, int slider, int edit) {					// 显示数值
	int position;
	CLOGD(TAG, "positionBBB显示 (%02x)(%02x) 数(%02x)", mKc3x->Trim[SPK_CH_CE], gValue[SPK_CH_CE], value);
	CString text;
	if ((value & 0x10) >= 9) {
		position = value & 0x0f;
		text.Format(L"%d", position * (-1));		// 显示负数
		((CStatic*)GetDlgItem(edit))->SetWindowText(text);
		return (9 + position);				// 滑块显示位置
		//CLOGD(TAG, "positionFFFF 负数(%d)位置(%d)", (position * (-1)), positionB);
		//((CSliderCtrl*)GetDlgItem(slider))->SetPos(positionB);
	}
	else {
		text.Format(L"%d", value);					// 显示正数	
		((CStatic*)GetDlgItem(edit))->SetWindowText(text);
		return (9 - value);				// 滑块显示位置
		//CLOGD(TAG, "positionTTT显示 (%02x)(%02x) 正数(%d)位置(%d) ", mKc3x->Trim[SPK_CH_CE], gValue[SPK_CH_CE], value, positionA);
		//((CSliderCtrl*)GetDlgItem(slider))->SetPos(positionA);
	}
}

void CPopTestTone::SetChannelTrim(int slider, int edit, KC3X_SPK_CHANNEL ch) {		// 读取选项的状态值
	int position = ((CSliderCtrl*)GetDlgItem(slider))->GetPos();
	uint8_t value;
	if (position >= 10) {
		value = 0x11 + (position - 10);								// 显示负数	
	}
	else {
		value = (position - 9) * (-1);								// 显示正数			
	}
	CLOGD(TAG, "position读取选项的状态值  (%02x)(%02x) 数(%02x)位置(%d)", mKc3x->Trim[SPK_CH_CE], gValue[SPK_CH_CE], value, position );
	gValue[ch] = value;
	ShowChannel(value, slider, edit);
	gSlowTime = 3;
}

void CPopTestTone::ShowTestTone(int value, int id0, int id1) {		// 显示噪音测试是否被选中
	CLOGE(TAG, "ShowTestTone 显示AAAAAAAA (%02x)(%d)(%02x)", mKc3x->TestTone, value, value);	
	if (value & 0x10){						// 打开噪音测试
		((CButton*)GetDlgItem(id0))->SetCheck(TRUE);
		((CButton*)GetDlgItem(id1))->SetCheck(FALSE);
		return;
	}
	else{									// 关闭噪音测试
		((CButton*)GetDlgItem(id0))->SetCheck(FALSE);
		((CButton*)GetDlgItem(id1))->SetCheck(TRUE);
		return;
	}	
}

int CPopTestTone::GetTestTone(int id0, int id1) {		// 读取选项的状态值
	int v0 = ((CButton*)GetDlgItem(id0))->GetCheck();
	int v1 = ((CButton*)GetDlgItem(id1))->GetCheck();
	return v0 ? 1 : 0;
}

void CPopTestTone::ShowRegister() {

	static const int tabIdsS[] = {
		IDC_SLIDER_TEST_TONE_FL,
		IDC_SLIDER_TEST_TONE_FR,
		IDC_SLIDER_TEST_TONE_CE,
		IDC_SLIDER_TEST_TONE_SW,
		IDC_SLIDER_TEST_TONE_SL,
		IDC_SLIDER_TEST_TONE_SR,
		IDC_SLIDER_TEST_TONE_BL,
		IDC_SLIDER_TEST_TONE_BR,
	};
	static const int tabIdsE[] = {
		IDC_STATIC_FL_VALUE,
		IDC_STATIC_FR_VALUE,
		IDC_STATIC_CE_VALUE,
		IDC_STATIC_SW_VALUE,
		IDC_STATIC_SL_VALUE,
		IDC_STATIC_SR_VALUE,
		IDC_STATIC_BL_VALUE,
		IDC_STATIC_BR_VALUE,
	};
	for (int channel = 0; channel < 8; channel++) {
		int position = ShowChannel(mKc3x->Trim[channel], tabIdsS[channel], tabIdsE[channel]);
		if (gSlowTime == 0) {		// 拉动滑块松手后才SetPos数值，防止拉动滑块反复跳动
			CLOGD(TAG, "ShowRegister position OnTimer EEE (%02x)(%02x)(%d)", mKc3x->Trim[SPK_CH_CE], gValue[SPK_CH_CE], position);
			((CSliderCtrl*)GetDlgItem(tabIdsS[channel]))->SetPos(position);
		}
	}

	int woofer = (mKc3x->TestTone & 0x10);										// 查看B4的数值
	ShowTestTone(woofer, IDC_RADIO_TEST_TONE_OPEN, IDC_RADIO_TEST_TONE_CLOSE);	// 显示噪音测试是否被选中


}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// C++语言对话框函数 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// CPopTestTone 对话框

IMPLEMENT_DYNAMIC(CPopTestTone, CDialogEx)

CPopTestTone::CPopTestTone(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_POP_TEST_TONE, pParent)
{
}

CPopTestTone::~CPopTestTone(){
}

void CPopTestTone::DoDataExchange(CDataExchange* pDX){
	CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CPopTestTone, CDialogEx)
	ON_WM_TIMER()
	ON_NOTIFY(NM_CUSTOMDRAW, IDC_SLIDER_TEST_TONE_CE, &CPopTestTone::OnNMCustomdrawSliderTestToneCe)
	ON_NOTIFY(NM_CUSTOMDRAW, IDC_SLIDER_TEST_TONE_SW, &CPopTestTone::OnNMCustomdrawSliderTestToneSw)
	ON_NOTIFY(NM_CUSTOMDRAW, IDC_SLIDER_TEST_TONE_FR, &CPopTestTone::OnNMCustomdrawSliderTestToneFr)
	ON_NOTIFY(NM_CUSTOMDRAW, IDC_SLIDER_TEST_TONE_SR, &CPopTestTone::OnNMCustomdrawSliderTestToneSr)
	ON_NOTIFY(NM_CUSTOMDRAW, IDC_SLIDER_TEST_TONE_BR, &CPopTestTone::OnNMCustomdrawSliderTestToneBr)
	ON_NOTIFY(NM_CUSTOMDRAW, IDC_SLIDER_TEST_TONE_LIPS, &CPopTestTone::OnNMCustomdrawSliderTestToneLips)
	ON_NOTIFY(NM_CUSTOMDRAW, IDC_SLIDER_TEST_TONE_BL, &CPopTestTone::OnNMCustomdrawSliderTestToneBl)
	ON_NOTIFY(NM_CUSTOMDRAW, IDC_SLIDER_TEST_TONE_SL, &CPopTestTone::OnNMCustomdrawSliderTestToneSl)
	ON_NOTIFY(NM_CUSTOMDRAW, IDC_SLIDER_TEST_TONE_FL, &CPopTestTone::OnNMCustomdrawSliderTestToneFl)
END_MESSAGE_MAP()

// CPopTestTone 消息处理程序
BOOL CPopTestTone::OnInitDialog(){
	CDialogEx::OnInitDialog();
	// TODO:  在此添加额外的初始化

	((CSliderCtrl*)GetDlgItem(IDC_SLIDER_TEST_TONE_CE))->SetTicFreq(9);	// 设置每x个刻度显示一个间隔刻度线(需要在属性“外观”打开“打勾标记”和“自动打勾”)
	((CSliderCtrl*)GetDlgItem(IDC_SLIDER_TEST_TONE_SW))->SetTicFreq(9);	// 设置每x个刻度显示一个间隔刻度线(需要在属性“外观”打开“打勾标记”和“自动打勾”)
	((CSliderCtrl*)GetDlgItem(IDC_SLIDER_TEST_TONE_FR))->SetTicFreq(9);	// 设置每x个刻度显示一个间隔刻度线(需要在属性“外观”打开“打勾标记”和“自动打勾”)
	((CSliderCtrl*)GetDlgItem(IDC_SLIDER_TEST_TONE_SR))->SetTicFreq(9);	// 设置每x个刻度显示一个间隔刻度线(需要在属性“外观”打开“打勾标记”和“自动打勾”)
	((CSliderCtrl*)GetDlgItem(IDC_SLIDER_TEST_TONE_BR))->SetTicFreq(9);	// 设置每x个刻度显示一个间隔刻度线(需要在属性“外观”打开“打勾标记”和“自动打勾”)
	((CSliderCtrl*)GetDlgItem(IDC_SLIDER_TEST_TONE_BL))->SetTicFreq(9);	// 设置每x个刻度显示一个间隔刻度线(需要在属性“外观”打开“打勾标记”和“自动打勾”)
	((CSliderCtrl*)GetDlgItem(IDC_SLIDER_TEST_TONE_SL))->SetTicFreq(9);	// 设置每x个刻度显示一个间隔刻度线(需要在属性“外观”打开“打勾标记”和“自动打勾”)
	((CSliderCtrl*)GetDlgItem(IDC_SLIDER_TEST_TONE_FL))->SetTicFreq(9);	// 设置每x个刻度显示一个间隔刻度线(需要在属性“外观”打开“打勾标记”和“自动打勾”)

	((CSliderCtrl*)GetDlgItem(IDC_SLIDER_TEST_TONE_CE))->SetRange(0, 18);	// 设置最小值和最大值
	((CSliderCtrl*)GetDlgItem(IDC_SLIDER_TEST_TONE_SW))->SetRange(0, 18);	// 设置最小值和最大值
	((CSliderCtrl*)GetDlgItem(IDC_SLIDER_TEST_TONE_FR))->SetRange(0, 18);	// 设置最小值和最大值
	((CSliderCtrl*)GetDlgItem(IDC_SLIDER_TEST_TONE_SR))->SetRange(0, 18);	// 设置最小值和最大值
	((CSliderCtrl*)GetDlgItem(IDC_SLIDER_TEST_TONE_BR))->SetRange(0, 18);	// 设置最小值和最大值
	((CSliderCtrl*)GetDlgItem(IDC_SLIDER_TEST_TONE_BL))->SetRange(0, 18);	// 设置最小值和最大值
	((CSliderCtrl*)GetDlgItem(IDC_SLIDER_TEST_TONE_SL))->SetRange(0, 18);	// 设置最小值和最大值
	((CSliderCtrl*)GetDlgItem(IDC_SLIDER_TEST_TONE_FL))->SetRange(0, 18);	// 设置最小值和最大值

	mCPopTestTone = this;
	isPopKc3xRegi = false;
	gSlowTime = 0;
	memset(gValue, 0, sizeof(gValue));
	ShowRegister();
	//isUserClick = false;		// 用户操作View的标志

	TestChangRegCallback(changRegCallback);
	SetTimer(1, 300, NULL);
	return TRUE;  // return TRUE unless you set the focus to a control
	// 异常: OCX 属性页应返回 FALSE
}

void CPopTestTone::OnTimer(UINT_PTR nIDEvent) {
	bool tt1 = GetTestTone(IDC_RADIO_TEST_TONE_OPEN, IDC_RADIO_TEST_TONE_CLOSE);
	bool tt2 = mKc3x->TestTone & 0x10;

	if (tt1 != tt2) {		// 
		mKc3x->TestTone = (tt1 == 1) ? 0x10 : 0x00;
		DntRegWrite();		// 写寄存器
		CLOGE(TAG, "ShowTestTone 显示CCCCCCC  (%d)(%02x)", mKc3x->TestTone, mKc3x->TestTone);
	}

	if ((mKc3x->TestTone & 0x10) == 0x10) {						// 如果打开噪音测试时
		int value;
		char bytes[8192];
		if (mKc3x->Trim[mKc3x->TestTone & 0x0f] >= 10) {
			value = (16 - mKc3x->Trim[mKc3x->TestTone & 0x0f]);	// 显示负数	
		}
		else {
			value = mKc3x->Trim[mKc3x->TestTone & 0x0f];		// 显示正数			
		}
		//CLOGE(TAG, "显示 KKKKK (%02x)(%02x)(%d)(%02x)(%d)", mKc3x->TestTone, (mKc3x->TestTone & 0x0f), mKc3x->Trim[mKc3x->TestTone & 0x0f], mKc3x->Trim[mKc3x->TestTone & 0x0f], value);
		int offset = TxtFormat(bytes, 512, "%s %ddB", (char*)Tab_DIP_ChannelShow[mKc3x->TestTone & 0x0f], value);
		bytes[offset] = 0;
		CString string(bytes);
		((CStatic*)GetDlgItem(IDC_STATIC_TEST_TONE_TEXT))->SetWindowText(string);
	}
	else {
		((CStatic*)GetDlgItem(IDC_STATIC_TEST_TONE_TEXT))->SetWindowText(L"已关闭噪音测试");
	}

	//if (isUserClick) {											// 用户操作View的标志
		if (memcmp(mKc3x->Trim, gValue, sizeof(gValue))) {		// 如果判断mKc3x->Trim与gValue数据不同
			//CLOGD(TAG, "position OnTimer AAA (%02x)(%02x) (%02x)(%02x)", mKc3x->Trim, gValue, mKc3x->Trim[SPK_CH_FL], gValue[SPK_CH_FL]);
			memcpy(mKc3x->Trim, gValue, sizeof(gValue));		// 把数据gValue复制到mKc3x->Trim中
			//CLOGD(TAG, "position OnTimer BBB (%02x)(%02x) (%02x)(%02x)", mKc3x->Trim, gValue, mKc3x->Trim[SPK_CH_FL], gValue[SPK_CH_FL]);
			isPopKc3xRegi = true;
			DntRegWrite();								// 写寄存器
		}
	//	isUserClick = false;		// 用户操作View的标志
	//}

	if (isPopKc3xRegi) {
		isPopKc3xRegi = false;
		//CLOGD(TAG, "position OnTimer CCC (%02x)(%02x)", mKc3x->Trim[SPK_CH_CE], gValue[SPK_CH_CE]);
		uint8_t save[8];
		memcpy(save, gValue, sizeof(gValue));		// 把gValue复制到save中，防止拉动滑块过程中数值反复跳动
		ShowRegister();
		memcpy(gValue, save, sizeof(gValue));		// 把save复制到gValue中，防止拉动滑块过程中数值反复跳动
		//CLOGD(TAG, "position OnTimer DDD (%02x)(%02x)", mKc3x->Trim[SPK_CH_CE], gValue[SPK_CH_CE]);
	}

	if (gSlowTime) {
		--gSlowTime;
	}

	CDialogEx::OnTimer(nIDEvent);
}
void CPopTestTone::OnNMCustomdrawSliderTestToneCe(NMHDR* pNMHDR, LRESULT* pResult){	// 中置
	LPNMCUSTOMDRAW pNMCD = reinterpret_cast<LPNMCUSTOMDRAW>(pNMHDR);
	SetChannelTrim(IDC_SLIDER_TEST_TONE_CE, IDC_STATIC_CE_VALUE,SPK_CH_CE);
	//isUserClick = true;			// 用户操作View的标志
	*pResult = 0;
}

void CPopTestTone::OnNMCustomdrawSliderTestToneSw(NMHDR* pNMHDR, LRESULT* pResult){	// 超低音
	LPNMCUSTOMDRAW pNMCD = reinterpret_cast<LPNMCUSTOMDRAW>(pNMHDR);
	SetChannelTrim(IDC_SLIDER_TEST_TONE_SW, IDC_STATIC_SW_VALUE, SPK_CH_SW);
	//isUserClick = true;			// 用户操作View的标志
	*pResult = 0;
}

void CPopTestTone::OnNMCustomdrawSliderTestToneFr(NMHDR* pNMHDR, LRESULT* pResult){	// 前置右
	LPNMCUSTOMDRAW pNMCD = reinterpret_cast<LPNMCUSTOMDRAW>(pNMHDR);
	SetChannelTrim(IDC_SLIDER_TEST_TONE_FR, IDC_STATIC_FR_VALUE, SPK_CH_FR);
	//isUserClick = true;			// 用户操作View的标志
	*pResult = 0;
}

void CPopTestTone::OnNMCustomdrawSliderTestToneSr(NMHDR* pNMHDR, LRESULT* pResult){	// 环绕右
	LPNMCUSTOMDRAW pNMCD = reinterpret_cast<LPNMCUSTOMDRAW>(pNMHDR);
	SetChannelTrim(IDC_SLIDER_TEST_TONE_SR, IDC_STATIC_SR_VALUE, SPK_CH_SR);
	*pResult = 0;
}

void CPopTestTone::OnNMCustomdrawSliderTestToneBr(NMHDR* pNMHDR, LRESULT* pResult){	// 后置右
	LPNMCUSTOMDRAW pNMCD = reinterpret_cast<LPNMCUSTOMDRAW>(pNMHDR);
	SetChannelTrim(IDC_SLIDER_TEST_TONE_BR, IDC_STATIC_BR_VALUE, SPK_CH_BR);
	//isUserClick = true;			// 用户操作View的标志
	*pResult = 0;
}

void CPopTestTone::OnNMCustomdrawSliderTestToneLips(NMHDR* pNMHDR, LRESULT* pResult){ // LIPS
	LPNMCUSTOMDRAW pNMCD = reinterpret_cast<LPNMCUSTOMDRAW>(pNMHDR);

	int position = ((CSliderCtrl*)GetDlgItem(IDC_SLIDER_TEST_TONE_LIPS))->GetPos();
	CString text;
	text.Format(L"%d", position);
	((CStatic*)GetDlgItem(IDC_STATIC_LIPS_VALUE))->SetWindowText(text);
	//isUserClick = true;			// 用户操作View的标志
	//ShowTestToneSlider(IDC_SLIDER_TEST_TONE_LIPS, IDC_STATIC_LIPS_VALUE);
	*pResult = 0;
}

void CPopTestTone::OnNMCustomdrawSliderTestToneBl(NMHDR* pNMHDR, LRESULT* pResult){	// 后置左
	LPNMCUSTOMDRAW pNMCD = reinterpret_cast<LPNMCUSTOMDRAW>(pNMHDR);
	SetChannelTrim(IDC_SLIDER_TEST_TONE_BL, IDC_STATIC_BL_VALUE, SPK_CH_BL);
	//isUserClick = true;			// 用户操作View的标志
	*pResult = 0;
}

void CPopTestTone::OnNMCustomdrawSliderTestToneSl(NMHDR* pNMHDR, LRESULT* pResult){	// 环绕左
	LPNMCUSTOMDRAW pNMCD = reinterpret_cast<LPNMCUSTOMDRAW>(pNMHDR);
	SetChannelTrim(IDC_SLIDER_TEST_TONE_SL, IDC_STATIC_SL_VALUE, SPK_CH_SL);
	//isUserClick = true;			// 用户操作View的标志
	*pResult = 0;
}

void CPopTestTone::OnNMCustomdrawSliderTestToneFl(NMHDR* pNMHDR, LRESULT* pResult){	// 前置左
	LPNMCUSTOMDRAW pNMCD = reinterpret_cast<LPNMCUSTOMDRAW>(pNMHDR);
	SetChannelTrim(IDC_SLIDER_TEST_TONE_FL, IDC_STATIC_FL_VALUE, SPK_CH_FL);
	//isUserClick = true;			// 用户操作View的标志
	*pResult = 0;
}
